<?php

/**
* @current column.php
* @author   LiuYang <liu_yang@haibao.com>
* @date 2017年2月9日 下午6:15:23
*/

namespace haibao\jiepai\data;
use \haibao\jiepai\model\filter\columns\CmsColumns as CmsColumnsFilter;
use \haibao\jiepai\data\CmsColumnsTags as CmsColumnsTagsData;
class Column extends \haibao\jiepai\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\jiepai\model\data\columns\CmsColumns');
    }
    
    public function addColumnData($addColumnModel){
        $this->beginTranscation();
        try{
            $this->add($addColumnModel);
            $this->addColumnTags($addColumnModel);
        } catch(\Exception $e){
            throw $e;
        }
        $this->endtranscation();
        return $addColumnModel;
    }
    
    public function updateColumnData($editColumnModel){
        $columnModel = $this->packColumnFilter($editColumnModel);
        $this->beginTranscation();
        try{
            $this->updateById($editColumnModel->Id,$columnModel);
            $this->updateColumnTags($editColumnModel);
        } catch(\Exception $e){
            throw $e;
        }
        $this->endtranscation();
    }

    /**
     * 保存快速评论列表
     */
    public function saveColumnQuickComment($columnId, $quickArr){
        $selectQuickList = array();
        if($quickArr){
            $str = preg_replace('/(\d+)/','%s',implode(',', $quickArr));
            array_push($quickArr, implode(',', $quickArr));
            $result = $this->query('select ID,Content from cms_comment_quick where ID in ('.$str.') order by find_in_set(ID, %s)', $quickArr);
            while($result->fetch_assoc()){
                $selectQuickList[$result->getData('ID')] = $result->getData('Content');
            }
        }
        $this->query('update cms_columns set QuickComment=%s where ColumnID=%s', array(
            json_encode($selectQuickList), $columnId
        ));
    }

    private function addColumnTags($addColumnModel){
        foreach ($addColumnModel->tagsJsonData as $value){
            $value->ColumnID = $addColumnModel->Id;
        }
        $cmsColumnsTagsModel = new CmsColumnsTagsData();
        $cmsColumnsTagsModel->addAll($addColumnModel->tagsJsonData);
    }
    private function packColumnFilter($editColumnModel){
        $cmsColumnsFilter = new CmsColumnsFilter();
        $cmsColumnsFilter = array(
            CmsColumnsFilter::CONDITION_FIELD_NAME_TOP_COLUMN_ID =>$editColumnModel->TopColumnID,
            CmsColumnsFilter::CONDITION_FIELD_NAME_DESCRIPTION =>$editColumnModel->Description,
            CmsColumnsFilter::CONDITION_FIELD_NAME_COLUMN_NAME =>$editColumnModel->ColumnName,
            CmsColumnsFilter::CONDITION_FIELD_NAME_COLUMN_NAME_FR =>$editColumnModel->ColumnNameFr,
            CmsColumnsFilter::CONDITION_FIELD_NAME_TAG_ID =>$editColumnModel->TagID,
            CmsColumnsFilter::CONDITION_FIELD_NAME_COLUMN_TYPE =>$editColumnModel->ColumnType,
            CmsColumnsFilter::CONDITION_FIELD_NAME_PICTURE_ID =>$editColumnModel->PictureID,
            CmsColumnsFilter::CONDITION_FIELD_NAME_TITLE_PICTURE_ID =>$editColumnModel->TitlePictureID,
            CmsColumnsFilter::CONDITION_FIELD_NAME_CREATE_TIME =>$editColumnModel->CreateTime,
            CmsColumnsFilter::CONDITION_FIELD_NAME_CREATOR_ID =>$editColumnModel->CreatorID,
            CmsColumnsFilter::CONDITION_FIELD_NAME_FLAG =>$editColumnModel->Flag,
            CmsColumnsFilter::CONDITION_FIELD_NAME_STATUS =>$editColumnModel->Status,
            CmsColumnsFilter::CONDITION_FIELD_NAME_SHOW_INDEX =>$editColumnModel->ShowIndex,
            CmsColumnsFilter::CONDITION_FIELD_NAME_UPDATE_DATE =>$editColumnModel->UpdateDate,
            CmsColumnsFilter::CONDITION_FIELD_NAME_TAGS =>$editColumnModel->Tags,
            CmsColumnsFilter::CONDITION_FIELD_NAME_TAGS_JSON =>$editColumnModel->TagsJson,
            CmsColumnsFilter::CONDITION_FIELD_NAME_UPDATE_DATE =>$editColumnModel->UpdateDate,
        ); 
        return $cmsColumnsFilter;
    }
    private function updateColumnTags($editColumnModel){
        $sql = "delete from cms_columns_tags where ColumnID=".$editColumnModel->Id;
        $this->query($sql);
        $cmsColumnsTagsData = new CmsColumnsTagsData();
        $cmsColumnsTagsData->addAll($editColumnModel->tagsJsonData);
    }
}
